package com.googlecode.stemwijzer.client.presenter;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.inject.Inject;
import com.googlecode.stemwijzer.client.event.AllQuestionsAnsweredEvent;
import com.googlecode.stemwijzer.client.event.ExceptionEvent;
import com.googlecode.stemwijzer.client.event.RegistrationEvent;
import com.googlecode.stemwijzer.client.event.ResetSessionEvent;
import com.googlecode.stemwijzer.client.event.handler.AllQuestionsAnsweredHandler;
import com.googlecode.stemwijzer.client.event.handler.ExceptionHandler;
import com.googlecode.stemwijzer.client.event.handler.RegistrationHandler;
import com.googlecode.stemwijzer.client.event.handler.ResetSessionHandler;

public class StemwijzerPresenter extends AbstractPresenter<StemwijzerPresenter.Display> implements RegistrationHandler,
		AllQuestionsAnsweredHandler, ExceptionHandler, ResetSessionHandler {

	public interface Display extends WidgetDisplay {
		HasWidgets getContainer();
	}

	public static final Place PLACE = new Place("StemwijzerPresenter");

	private NotificationPresenter notificationPresenter;
	private RegistrationPresenter registrationPresenter;
	private AnswerQuestionPresenter answerQuestionPresenter;
	private ResultsPresenter resultsPresenter;

	@Inject
	public StemwijzerPresenter(final Display display, final EventBus eventBus,
			final NotificationPresenter notificationPresenter, final RegistrationPresenter registrationPresenter,
			final AnswerQuestionPresenter answerQuestionPresenter, final ResultsPresenter resultsPresenter) {
		super(display, eventBus);

		this.notificationPresenter = notificationPresenter;
		this.registrationPresenter = registrationPresenter;
		this.answerQuestionPresenter = answerQuestionPresenter;
		this.resultsPresenter = resultsPresenter;

		bind();
	}

	@Override
	protected void onBind() {
		revealDisplay();

		this.eventBus.addHandler(RegistrationEvent.TYPE, this);
		this.eventBus.addHandler(AllQuestionsAnsweredEvent.TYPE, this);
		this.eventBus.addHandler(ResetSessionEvent.TYPE, this);
		this.eventBus.addHandler(ExceptionEvent.TYPE, this);
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	public void revealDisplay() {
		Log.debug("Displaying registration");

		display.getContainer().add(registrationPresenter.getDisplay().asWidget());
	}

	@Override
	public void onRegistration(RegistrationEvent registratieEvent) {
		Log.debug("Handling registration event");

		display.getContainer().clear();
		display.getContainer().add(answerQuestionPresenter.getDisplay().asWidget());

		answerQuestionPresenter.revealDisplay();
	}

	@Override
	public void onAllQuestionsAnswered(AllQuestionsAnsweredEvent event) {
		Log.debug("Handling all questions answered event");

		display.getContainer().clear();
		display.getContainer().add(resultsPresenter.getDisplay().asWidget());

		resultsPresenter.revealDisplay();
	}

	@Override
	public void onResetSession(ResetSessionEvent event) {
		Log.debug("Handling reset session event");

		display.getContainer().clear();
		display.getContainer().add(answerQuestionPresenter.getDisplay().asWidget());

		answerQuestionPresenter.initialize();
		answerQuestionPresenter.revealDisplay();
	}

	@Override
	public void onException(ExceptionEvent event) {
		Log.debug("Handling exception event");

		notificationPresenter.onException(event);
	}
}
